//
//  AddViewController.m
//  Article
//
//  Created by LONG on 16/4/22.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "AddViewController.h"
#import "EditViewController.h"

@interface AddViewController ()<UIWebViewDelegate>

@end

@implementation AddViewController
//{
//    UIWebView *_webview;
//
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationController.navigationBar setBarTintColor:UIColorFromRGB(MYColor)];
    // Do any additional setup after loading the view from its nib.
}
- (IBAction)addView:(UIButton *)sender {
    
    
    if ([[[[UIPasteboard generalPasteboard] string] substringToIndex:5] isEqualToString:@"http:"]) {
        //正确的URL
        NSLog(@"粘贴的URL: %@",[[UIPasteboard generalPasteboard] string]);
        //[self reyion:[[UIPasteboard generalPasteboard] string]];
        [self reyionHtmlTitle:[[UIPasteboard generalPasteboard] string]];
        
        //            NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[[UIPasteboard generalPasteboard] string]]];
        //
        //            _webview = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 0,0)];
        //            [_webview loadRequest:request];
        //            _webview.delegate = self;
        
        
    }else{
        //错误的URL
        
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"请粘贴微信或微博文章链接" message:[[UIPasteboard generalPasteboard] string] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
    }
}

//- (void)webViewDidFinishLoad:(UIWebView *)webView
//{
//    UIWebView *web = webView;
//    
//    //获取所有的html
//    
//    //NSString *allHtml = @"document.documentElement.innerHTML";
//    
//    //获取网页title
//    
//    NSString *htmlTitle = @"document.title";
//    
//    //获取网页的一个值
//    
//    NSString *htmlNum = @"document.getElementById('js_content').innerHTML";
//    // @"document.getElementsByName('q')[0].value='朱祁林';"
//    //获取到得网页内容
//    
//    //NSString *allHtmlInfo = [web stringByEvaluatingJavaScriptFromString:allHtml];
//    
//    //NSLog(@"???????????%@",allHtmlInfo);
//    
//    NSString *titleHtmlInfo = [web stringByEvaluatingJavaScriptFromString:htmlTitle];
//    
//    //NSLog(@">>>>>>>>>>%@",titleHtmlInfo);
//    
//    NSString *numHtmlInfo = [web stringByEvaluatingJavaScriptFromString:htmlNum];
//    
//    //NSLog(@"}}}}}}}}}}}}}%@",numHtmlInfo);
//    [self reyionHtmlTitle:titleHtmlInfo numHtmlInfo:numHtmlInfo];
//    
//}

- (void)reyionHtmlTitle:(NSString *)htmlTitle{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic = @{@"user_token":[user objectForKey:@"token"],
                          @"url":htmlTitle
                          };
    
    NSLog(@"%@",dic);
    AFHTTPRequestOperationManager *manager = [MyTools initAFHttpManager];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [manager POST:GETARTICLE_URL parameters:dic success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        if ([[responseObject objectForKey:@"status"] intValue] == 1) {
            [self.view makeToast:[responseObject objectForKey:@"info"] duration:1.0 position:CSToastPositionCenter];
            NSLog(@"%@",responseObject);
            EditViewController *evc= [[EditViewController alloc]init];
            evc.id = [responseObject objectForKey:@"id"];
            evc.writing_title = [responseObject objectForKey:@"title"];
            
            evc.writing_img = [responseObject objectForKey:@"writing_default_img"];
            [self.navigationController pushViewController:evc animated:YES];
            
        }else{
            [self.view makeToast:[responseObject objectForKey:@"info"] duration:1.0 position:CSToastPositionCenter];
        }
        
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        if ([MyTools connectedToNetwork] == 0) {
            [self.view makeToast:@"请检查是否联网" duration:1.0 position:CSToastPositionCenter];
        }else{
            [self.view makeToast:@"服务器错误，再试一次" duration:1.0 position:CSToastPositionCenter];
        }
    }];
}

@end
